package com.situ.firmadmin.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import com.situ.firmadmin.mapper.DeptMapper;
import com.situ.firmadmin.mapper.RoleTypeMapper;
import com.situ.firmadmin.mapper.UserMapper;
import com.situ.firmadmin.pojo.Dept;
import com.situ.firmadmin.pojo.RoleType;
import com.situ.firmadmin.pojo.User;
import com.situ.firmadmin.pojo.vo.UserDeptVO;
import org.springframework.beans.BeanUtils;

public class UserExcelListener extends AnalysisEventListener<UserDeptVO> {
	
	private DeptMapper deptMapper;
	private UserMapper userMapper;
	private RoleTypeMapper roleTypeMapper;
	public UserExcelListener(UserMapper userMapper,DeptMapper deptMapper,RoleTypeMapper roleTypeMapper) {
		this.userMapper = userMapper;
		this.deptMapper = deptMapper;
		this.roleTypeMapper = roleTypeMapper;
	}
	
	@Override
	public void invoke(UserDeptVO userDeptVO, AnalysisContext analysisContext) {
		User user=new User();
		BeanUtils.copyProperties(userDeptVO,user);
		Dept dept=deptMapper.selectByName(userDeptVO.getDeptName());
		if(userDeptVO.getRole()!=null && userDeptVO.getRole()!=""){
			RoleType roleType=roleTypeMapper.selectByName(userDeptVO.getRole());
			user.setRole(roleType.getId());
		}
		user.setDepartmentId(dept.getId());
		user.setId(null);
		userMapper.insert(user);
	}
	
	@Override
	public void doAfterAllAnalysed(AnalysisContext analysisContext) {
	
	}
}
